<template>
  <div class="logon">
    <h3>面经登录</h3>
    <hr />
    <form v-if="isShow">
      <div class="input-group">
        用户名
        <input
          type="text"
          id="username"
          v-model="username"
          placeholder="用户名"
        />
      </div>
      <hr />
      <div class="input-group">
        <label for="password">密码</label>
        <input
          type="password"
          id="password"
          placeholder="密码"
          v-model="password"
        />
      </div>
      <hr />
    </form>
    <div v-else>
      <van-cell-group>
        <van-field
          v-model="username"
          label="用户名"
          placeholder="请输入用户名"
          error-message="用户名不能为空字符长度不不能小于6"
        />
        <hr />

        <van-field
          v-model="password"
          label="密码"
          placeholder="请输入密码"
          error-message="密码不能为空字符长度不不能小于6"
        />
        <hr />
      </van-cell-group>
    </div>
    <button type="submit" @click="fn">提交</button>
    <span @click="hh">注册账号</span>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      username: "",
      password: "",
      isShow: true,
      token: "chengfeng",
      res: null,
    };
  },
  methods: {
    async fn() {
      if (this.username.trim() === "" || this.username.length <= 6) {
        this.isShow = false;
      } else if (this.password.trim() === "" || this.password.length < 6) {
        this.isShow = false;
      } else {
        this.res = await axios({
          url: "http://interview-api-t.itheima.net/h5/user/login",
          method: "post",
          headers: { "Content-Type": "application/json" },
          data: {
            username: this.username,
            password: this.password,
          },
        });
        // console.log(this.res.data);
        localStorage.setItem("token", this.res.data.data.token);
      }
      if (this.res.data.code === 10000) {
        // console.log("提交成功");
        this.$router.push({ path: "/home" });
        this.username = "";
        this.password = "";
      }
    },
    hh() {
      this.$router.push({ path: "/register" });
    },
  },
};
</script>

<style scoped lang="less">
h3 {
  text-align: center;
}
.logon {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}

.input-group {
  margin-bottom: 15px;
}

input[type="text"],
input[type="password"] {
  padding: 8px;
  /* box-sizing: border-box; */
  border: none;
  margin-left: 30px;
  outline: none;

  height: 30px;
}

input[type="password"] {
  margin-left: 50px;
}
button {
  width: 100%;
  padding: 10px;
  background-color: orange;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  margin-top: 10px;
}

from {
  padding: 0 10px;
}
span {
  color: #0056b3;
  margin-top: 10px;
  float: right;
}
</style>
